package org.korifey.ffbdd.samples.logic;

import java.util.Arrays;
import java.util.regex.Pattern;

public class Token {
	enum TYPE {
		VARIABLE,
		CONST,
		OP,
		LPAR,
		RPAR
		
	}
	
	TYPE type;
	String lexem;
	int idx;
	
	public Token(TYPE type, String lexem, int idx) {
		super();
		this.type = type;
		this.lexem = lexem;
		this.idx = idx;
		validate();
	}
	public TYPE getType() {
		return type;
	}
	public String getLexem() {
		return lexem;
	}
	
	
	
	public int getIdx() {
		return idx;
	}
	private void err(String msg) throws ParseException {
		throw new ParseException("Error during parsing token "+getType()+"(lexem:'"+lexem+"') at pos '"+idx+"': "+msg);
	}
	
	public void validate() throws ParseException {
		if (lexem == null || lexem.isEmpty()) err("corresponding lexem is empty");
		
		switch (type) {
		case CONST:
			if (lexem.length() > 1 || (lexem.charAt(0) != '0' && lexem.charAt(0) != '1')) 
				err("only 1 or 0 are allowed as constants");
			break;
		case OP:
			if (Operation.TYPE.getType(lexem) == null) err("unregister operation, allowed operations are:"+
					Arrays.toString(Operation.TYPE.getLexems()));
			break;
		default:
			break;
		}
	}
	
}
